<?php
plugin('admin-top-permissions');
$perUrl = PERMISSIONS_URL;
$pergUrl = GROUPS_URL;
$per = unserialize(file_get_contents($perUrl));
if($_POST['addGroup']) {
	if(array_key_exists($_POST['groupName'], $per))
		$smess = $admin->message('error', 'Group by that Name Exists', 'A group by that name already exists.');
	$newGroup = $per[$_POST['inherit']];
	$newArr = array($_POST['groupName'] => $newGroup);
	$newPer = array_merge($per, $newArr);
	file_put_contents($perUrl, serialize($newPer));
	file_put_contents($pergUrl, file_get_contents($pergUrl)."\n".$_POST['groupName']);
	$messages .= $admin->message('success', 'Group Added', 'This group was given the permissions of user by default');
}
if($_POST['deleteGroup']) {
	$groups = $admin->getGroups();
	foreach($groups as $key => $value) {
		if($value == $_POST['groupName'])
			unset($groups[$key]);
	}
	unset($per[$_POST['groupName']]);
	file_put_contents($perUrl, serialize($per));
	file_put_contents($pergUrl, implode("\n", $groups));
	$messages .= $admin->message('success', 'Group Deleted', 'This group was deleted.');
	
}
if($_POST['save']) {
	foreach($_POST as $key => $value) {
		if($key !== 'save') {
			list($level, $cat, $item) = explode('_', $key);
			$level = str_replace('-', '', $level);
			$cat = str_replace('-', ' ', $cat);
			$item = str_replace('-', ' ', $item);
			if($value == 'on')
				$value = true;
			else
				$value = false;
			if(!is_array($newPer[$level]))
				$newPer[$level] = array();
			if(!is_array($newPer[$level][$cat]))
				$newPer[$level][$cat] = array();
			$newPer[$level][$cat][$item] = $value;
		}
	}
	foreach($newPer as $level => $arrtrue) {
		foreach($arrtrue as $cat => $arr2) {
			foreach($arr2 as $thing => $bool) {
				if(empty($newPer[$level][$cat][$thing]))
					$newArr[$level][$cat][$thing] = false;
				else
					$newArr[$level][$cat][$thing] = true;
			}
		}
	}
	file_put_contents($perUrl, serialize($newArr));
	$messages .= $admin->message('success', 'Permissions Saved', 'Users were updated.');
}
if(!is_writable($perUrl)) {
	$permisUrl = explode(BASE_URL, PERMISSIONS_URL);
	$messages .= $admin->message('error', 'File Not Writable', 'The file '.$permisUrl.' needs to be writable (chmod 755 or 777)');
}
$perUrl = PERMISSIONS_URL;
$per = unserialize(file_get_contents($perUrl));

$levels = array_keys($per);
$head = '';
$output .= "<form action='admin.php?page=permissions' method='post'>
<fieldset>
<h2>Permissions</h2>
<p>This allows you to change the permissions for a particular group of users. Any page disables will override individual actions. For example: If the page Edit and Delete Pages is disabled, then even if the actions Edit Page or Delete Page are enabled, the user won't be able to edit or delete pages.</p>
<input type='submit' name='save' value='Save Permissions' />
<table id='permissions' cellspacing='0' cellborder='0'>
<thead>
	<tr><th>&nbsp;</th>";
foreach($levels as $value) {
	$output .= "<th>$value</th>";
}
$output .= "</tr></thead><tbody>";
foreach($per['admin'] as $cat => $arr) {
	$output .= "<tr class='head'><td colspan='50'>$cat</td></tr>";
	foreach($arr as $key => $value) {
		$output .= "<tr><td>$key</td>";
		foreach($levels as $level) {
			if($per[$level][$cat][$key])
				$output .= "<td><input type='checkbox' name='".str_replace(' ', '-', $level)."_".str_replace(' ', '-', $cat)."_".str_replace(' ', '-', $key)."' checked='checked' /></td>";
			else
				$output .= "<td><input type='checkbox' name='".str_replace(' ', '-', $level)."_".str_replace(' ', '-', $cat)."_".str_replace(' ', '-', $key)."' /></td>";
		}
		$output .= "</tr>";
	}
}
$output .= "</tbody></table><input type='submit' name='save' value='Save Permissions' /></fieldset></form>";
$options = $admin->getGroupsOptions();
$output .= <<<EOT
<form action='admin.php?page=permissions' method='post'><fieldset><h2>Add a User Group</h2>
$smess
<div class='form-row'>
	<label for='groupName'>Group Name</label>
	<span>
		<input type="text" name="groupName" />
	</span>
</div>
<div class='form-row'>
	<label for='groupName'>Copy Permissions From</label>
	<span>
		<select name='inherit'>
			$options
		</select>
	</span>
</div>
<div class='form-row form-row-last'>
	<label for='addGroup'>Add</label>
	<span>
		<input type="submit" name="addGroup" value="Go!" />
	</span>
</div>
</fieldset>
</form>
EOT;
$groups = $admin->getGroupsOptions();
$output .= <<<EOT
<form action='admin.php?page=permissions' method='post'><fieldset><h2>Delete User Group</h2>
$smess2
<div class='form-row'>
	<label for='groupName'>Group</label>
	<span>
		<select name="groupName">
			$groups
		</select>
	</span>
</div>
<div class='form-row form-row-last'>
	<label for='deleteGroup'>Delete</label>
	<span>
		<input type="submit" name="deleteGroup" value="Go!" />
	</span>
</div>
</fieldset>
</form>
EOT;
$admin->page_info['content']['page_title'] = "Permissions";
$admin->page_info['content']['page_content']	= $head.$messages.$output;
plugin('admin-bottom-permissions');
//Display page
include(BASE_URL.'includes/admin/admin.php');
?>